package com.yc.yyds.servlet;

import com.google.gson.Gson;
import com.yc.yyds.Bean.Result;
import com.yc.yyds.dao.Userdao;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;

@WebServlet(name = "SaveUserServlet", value = "/SaveUserServlet.s")
public class SaveUserServlet extends HttpServlet {
    Userdao dao=new Userdao();
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        response.setCharacterEncoding("utf-8");
        response.setContentType("text/html;charset=utf-8");

        String username = request.getParameter("username");
        String userzh = request.getParameter("userzh");
        String pwd = request.getParameter("pwd");
        String phone = request.getParameter("phone");
        String times = request.getParameter("time");
        System.out.println("times = " + times);
        String email = request.getParameter("email");
        String image = request.getParameter("image");
        String isvip = request.getParameter("isvip");

        String userid = request.getParameter("userid");
        Result result;
        try {
            if(userid!=null){
                dao.modify(userid,username,userzh,pwd,phone,times,email,isvip,image);
            }else{
                dao.create(username,userzh,pwd,phone,times,email,isvip,image);
            }
            result=new Result(1,"用户保存成功",null);
        }catch (Exception E){
            E.printStackTrace();
            result=new Result(0,"用户保存失败",null);
        }
        String s = new Gson().toJson(result);
        response.getWriter().append(s);
    }
}
